package cn.action.member;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.struts2.ServletActionContext;

import cn.action.BaseAction;
import cn.beans.Member;
import cn.utils.PageModel;
import cn.utils.StringUtil;

@SuppressWarnings("unchecked")
public class MemberAction extends BaseAction {
	private static final long serialVersionUID = 1L;
	private Member member;
	private String memberId;
	private String username;
	private String password;
	private String rePassword;
	private String errorinfo;
	private String keywords;
	private int currentPage;
	private int pageSize;
	private PageModel pm = new PageModel();
	
	
	/**
	 * 登陆
	 * @throws IOException 
	 */
	public void login() throws IOException{
		try {
			String hql="from Member o where  o.username='"+username+"' ";
			List<Member> list=findSomeByHQL.findDataByHql(hql);
			if(StringUtil.isAllEmpty(username,password)){
				super.sendMsgAjax("isNull", null);
			}else{
				if(null!=list&&!list.isEmpty()){
					member =list.get(0);
					if(password.trim().equals(member.getPassword())){
						super.sendMsgAjax("success", null);
						Map session=ServletActionContext.getContext().getSession();
						session.put("memberId", member.getId());
						session.put("memberName", member.getUsername());
					}else{
						super.sendMsgAjax("error", null);
					}
				}
				super.sendMsgAjax("error", null);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			super.sendMsgAjax("error", null);
		}
		
	}
	
	/**
	 * 退出登陆
	 * @throws IOException 
	 */
	public void logout() throws IOException{
		try {
			Map session=ServletActionContext.getContext().getSession();
			session.put("memberId", null);
			session.put("memberName", null);
			super.sendMsgAjax("success", null);

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			super.sendMsgAjax("success", null);
		}
		
	}
	
	/**
	 * 检测用户是否存在
	 * @param username
	 * @return 用户存在则返回false,不存在则返回true
	 */
	private boolean checkExist(String username){
		boolean tag = true;
		String hql = "FROM Member AS o WHERE o.username = ? ";
		Object[] param = { username };
		List listMember = findSomeByHQL.findDataByHql(hql, param);
		if(listMember.size()!=0){
			tag = false;
		}
		return tag;
	}
	
	/**
	 * 通过ajax检测用户名是否已存在
	 * @throws Exception
	 */
	public void checkUsername() throws Exception{
		boolean tag = this.checkExist(username);
		if(tag){
			this.sendMsgAjax("ok", null);
		}else{
			this.sendMsgAjax("exist", null);
		}
	}
	
	/**
	 * 添加会员员
	 * @throws Exception
	 */
	public void addMember() throws Exception{
		try{
			if(null == member || null == member.getUsername() || "".equals(member.getUsername().trim())){
				this.sendMsgAjax("username", null);
			}else if(null == password || "".equals(password.trim()) 
				|| null == rePassword || "".equals(rePassword.trim())){
				this.sendMsgAjax("password", null);
			}else if(!password.equals(rePassword)){
				this.sendMsgAjax("notTheSame", null);
			}else{
				if(checkExist(member.getUsername())){
					member.setPassword(password);
					commonservice.saveDemo(member);
					this.sendMsgAjax("success", null);
				}else{
					this.sendMsgAjax("exist", null);
				}
			}
		}catch (Exception e) {
			e.printStackTrace();
			this.sendMsgAjax("error", null);
		}
	}
	
	/**
	 * 删除管理员
	 */
	public void deleteMember() throws Exception{
		try{
			String[] ids = memberId.split(",");
			int length = ids.length;
			for(int i=0;i<length;i++){
				Member m = commonservice.findById(ids[i], new Member());
				if(null != m){
					commonservice.deleteDemo(m, null);
				}
			}
			this.sendMsgAjax("success", null);
		}catch (Exception e) {
			e.printStackTrace();
			this.sendMsgAjax("error", null);
		}
	}
	
	/**
	 * 修改会员信息
	 * @throws Exception
	 */
	public void updateMember() throws Exception{
		try{
			if(null == member.getId() || "".equals(member.getId().trim())){
				this.sendMsgAjax("username", null);
			}else{
				Member m = commonservice.findById(member.getId(), new Member());
				if(null == m){
					this.sendMsgAjax("notExist", null);
				}else{
					if(null != password && !"".equals(password)){
						m.setPassword(password);
					}
					m.setUsername(member.getUsername());
					m.setEmail(member.getEmail());
					m.setMobile(member.getMobile());
					m.setMsn(member.getMsn());
					m.setName(member.getName());
					m.setQq(member.getQq());
					m.setTelephone(member.getTelephone());
					commonservice.updateDemo(m);
					this.sendMsgAjax("success", null);
				}
			}
		}catch (Exception e) {
			e.printStackTrace();
			this.sendMsgAjax("error", null);
		}
	}
	
	/**
	 * 通过ID查询会员
	 * @return
	 */
	public String memberDetail(){
		try {
			member = commonservice.findById(memberId, new Member());
			if(null != member ){
				return SUCCESS;
			}
			return null;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	/**
	 * 列表显示会员
	 * @return
	 */
	public String listMember(){
		try {
			pm.setCurrentPage(currentPage);
			pm.setPageSize(pageSize);
			
			List listOne = new ArrayList();
			String[] propertyName = { "username" };
			String[] propertyCon = { "!=" };
			Object[] propertyValue = { "admin" };
			String[] condition = null;
			listOne.add(propertyName);
			listOne.add(propertyCon);
			listOne.add(propertyValue);
			listOne.add(condition);

			List listTwo = new ArrayList();
			String[] propertyNameQuery = {"id","username","name","telephone","mobile","email","qq","msn" };
			String[] propertyConQuery = { "like","like","like","like","like","like","like","like" };
			Object[] propertyValueQuery = { keywords,keywords,keywords,keywords,keywords,keywords,keywords,keywords };
			String[] conditionQuery = { "OR","OR","OR","OR","OR","OR","OR" };
			listTwo.add(propertyNameQuery);
			listTwo.add(propertyConQuery);
			listTwo.add(propertyValueQuery);
			listTwo.add(conditionQuery);

			String[] orderPropertyName = { "id" };
			String[] orderValue = { "ASC" };

			pm = commonservice.findDataByPage(pm, "Member", listOne, "AND", listTwo, null, null, null, null, orderPropertyName, orderValue);

			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getRePassword() {
		return rePassword;
	}

	public void setRePassword(String rePassword) {
		this.rePassword = rePassword;
	}

	public String getErrorinfo() { 
		return errorinfo;
	}

	public void setErrorinfo(String errorinfo) {
		this.errorinfo = errorinfo;
	}

	public String getKeywords() {
		return keywords;
	}

	public void setKeywords(String keywords) {
		this.keywords = keywords;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public PageModel getPm() {
		return pm;
	}

	public void setPm(PageModel pm) {
		this.pm = pm;
	}

	public Member getMember() {
		return member;
	}

	public void setMember(Member member) {
		this.member = member;
	}

	public String getMemberId() {
		return memberId;
	}

	public void setMemberId(String memberId) {
		this.memberId = memberId;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}
	
	
}
